package com.zhangpingyang.jwttoken.map;

import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @Author: Zhang Pingyang
 * @Date: 2019/11/21 14:25
 */
public abstract class MyAbstractMap<K, V> implements MyMap<K, V> {

    protected MyAbstractMap() {
    }

    public abstract Set<MyEntry<K, V>> entrySet();

    public int size() {
        return entrySet().size();
    }

    public boolean isEmpty() {
        return size() == 0;
    }

    @Override
    public boolean containsValue(Object value) {
        Iterator<MyEntry<K, V>> iterator = entrySet().iterator();
        if (value == null) {
            while (iterator.hasNext()) {
                MyEntry<K, V> e = iterator.next();
                if (e.getValue() == null) {
                    return true;
                }
            }
        } else {
            while (iterator.hasNext()) {
                MyEntry<K, V> e = iterator.next();
                if (value.equals(e.getValue())) {
                    return true;
                }
            }
        }
        return false;
    }

    public boolean containsKey(Object key) {
        Iterator<MyEntry<K, V>> iterator = entrySet().iterator();
        if (key == null) {
            while (iterator.hasNext()) {
                MyEntry<K, V> next = iterator.next();
                if (next.getKey() == null) {
                    return true;
                }
            }
        } else {
            while (iterator.hasNext()) {
                MyEntry<K, V> next = iterator.next();
                if (key.equals(next.getKey())) {
                    return true;
                }
            }
        }
        return false;
    }

    public V get(Object key) {
        Iterator<MyEntry<K, V>> i = entrySet().iterator();
        if (key == null) {
            while (i.hasNext()) {
                MyEntry<K, V> next = i.next();
                if (next.getKey() == null) {
                    return next.getValue();
                }
            }
        } else {
            while (i.hasNext()) {
                MyEntry<K, V> next = i.next();
                if (key.equals(next.getKey())) {
                    return next.getValue();
                }
            }
        }
        return null;
    }

    public V put(K key, V value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public V remove(Object key) {
        Iterator<MyEntry<K, V>> iterator = entrySet().iterator();
        MyEntry<K, V> correctEntry = null;
        if (key == null) {
            while (correctEntry == null && iterator.hasNext()) {
                MyEntry<K, V> next = iterator.next();
                if (next.getKey() == null) {
                    correctEntry = next;
                }
            }
        } else {
            while (correctEntry == null && iterator.hasNext()) {
                MyEntry<K, V> next = iterator.next();
                if (key.equals(next.getKey())) {
                    correctEntry = next;
                }
            }
        }
        V oldValue = null;
        if (correctEntry != null) {
            oldValue = correctEntry.getValue();
            iterator.remove();
        }
        return oldValue;
    }

    @Override
    public void putAll(MyMap<? extends K, ? extends V> map) {
        for (MyEntry<? extends K, ? extends V> myEntry : map.entrySet()) {
            put(myEntry.getKey(), myEntry.getValue());
        }
    }

    @Override
    public void clear() {
        entrySet().clear();
    }

    transient volatile Set<K> keySet = null;
    transient volatile Collection<V> values = null;

    @Override
    public Set<K> keySet() {
        if (keySet == null) {
            keySet = new AbstractSet<K>() {
                public Iterator<K> iterator() {
                    return new Iterator<K>() {
                        private Iterator<MyEntry<K, V>> i = entrySet().iterator();
                        @Override
                        public boolean hasNext() {
                            return i.hasNext();
                        }
                        @Override
                        public K next() {
                            return i.next().getKey();
                        }
                        @Override
                        public void remove() {
                            i.remove();
                        }
                    };
                }
                @Override
                public int size() {
                    return MyAbstractMap.this.size();
                }
                @Override
                public boolean isEmpty() {
                    return MyAbstractMap.this.isEmpty();
                }
                @Override
                public void clear() {
                    MyAbstractMap.this.clear();
                }
                @Override
                public boolean contains(Object o) {
                    return MyAbstractMap.this.containsKey(o);
                }
            };
        }
        return keySet;
    }

    @Override
    public Collection<V> values() {
        if (values == null) {
            values = new AbstractCollection<V>() {
                Iterator<MyEntry<K, V>> i = entrySet().iterator();

                @Override
                public Iterator<V> iterator() {
                    return new Iterator<V>() {
                        @Override
                        public boolean hasNext() {
                            return i.hasNext();
                        }

                        @Override
                        public V next() {
                            return i.next().getValue();
                        }
                    };
                }

                @Override
                public int size() {
                    return MyAbstractMap.this.size();
                }

                @Override
                public boolean isEmpty() {
                    return MyAbstractMap.this.isEmpty();
                }

                @Override
                public void clear() {
                    MyAbstractMap.this.clear();
                }

                @Override
                public boolean contains(Object o) {
                    return MyAbstractMap.this.containsValue(o);
                }
            };
        }
        return values;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof Map)) {
            return false;
        }
        Map<K, V> m = (Map<K, V>) o;
        if (m.size() != size()) {
            return false;
        }
        try {
            Iterator<MyEntry<K, V>> i = entrySet().iterator();
            while (i.hasNext()) {
                MyEntry<K, V> e = i.next();
                K key = e.getKey();
                V value = e.getValue();
                if (value == null) {
                    if (!(m.get(key) == null && m.containsKey(key))) {
                        return false;
                    }
                } else {
                    if (!(value.equals(m.get(key)))) {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int h = 0;
        Iterator<MyEntry<K, V>> i = entrySet().iterator();
        while (i.hasNext()) {
            h += i.next().hashCode();
        }
        return h;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        MyAbstractMap<K, V> clone = (MyAbstractMap<K, V>) super.clone();
        clone.keySet = null;
        clone.values = null;
        return clone;
    }

    private static boolean eq(Object o1, Object o2) {
        return Objects.equals(o1, o2);
    }

    public static class SimpleEntry<K, V> implements MyEntry<K, V>, Serializable {

        private static final long serialVersionUID = 3316713520667793641L;

        private final K key;
        private V value;

        public SimpleEntry(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public SimpleEntry(MyEntry<? extends K, ? extends V> entry) {
            this.key = entry.getKey();
            this.value = entry.getValue();
        }
        @Override
        public K getKey() {
            return this.key;
        }
        @Override
        public V getValue() {
            return this.value;
        }
        @Override
        public V setValue(V value) {
            V oldValue = this.value;
            this.value = value;
            return oldValue;
        }
        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof MyMap.MyEntry)) {
                return false;
            }
            MyMap.MyEntry e = (MyEntry) obj;
            return eq(key, e.getKey()) && eq(value, e.getValue());
        }

        @Override
        public int hashCode() {
            return (key == null ? 0 : key.hashCode()) ^
                    (value == null ? 0 : value.hashCode());
        }

        @Override
        public String toString() {
            return key + "=" + value;
        }
    }

    public static class SimpleImmutableEntry<K, V> implements MyEntry<K, V>, Serializable {

        private static final long serialVersionUID = 635385667517542853L;

        private final K key;
        private final V value;

        public SimpleImmutableEntry(K key, V value) {
            this.key = key;
            this.value = value;
        }

        public SimpleImmutableEntry(MyEntry<? extends K, ? extends V> entry) {
            this.key = entry.getKey();
            this.value = entry.getValue();
        }

        @Override
        public K getKey() {
            return key;
        }

        @Override
        public V getValue() {
            return value;
        }

        @Override
        public V setValue(V value) {
            throw new UnsupportedOperationException();
        }

        @Override
        public boolean equals(Object obj) {
            if (!(obj instanceof MyMap.MyEntry)) {
                return false;
            }
            MyMap.MyEntry e = (MyEntry) obj;
            return eq(key, e.getKey()) && eq(value, e.getValue());
        }
        public int hashCode() {
            return (key == null ? 0 : key.hashCode()) ^
                    (value == null ? 0 : value.hashCode());
        }

        @Override
        public String toString() {
            return key + "=" + value;
        }
    }


}
